﻿using System;
using System.Text.RegularExpressions;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Housing;
using Microsoft.ApplicationBlocks.Data;

public partial class HouseKeep_ManageAdmissibility : System.Web.UI.Page
{
    Admissibility admissibility = new Admissibility();
    Apply apply = new Apply();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack && Session["UserId"] != null)
        {
            PageBind();
            BindDate();
        }
    }

    protected void BindDate()
    {
        this.ddlMonth.Items.Clear();
        this.ddlMonth.Items.Add("全部");
        for (int i =1; i <=12; i++)
        {
                  
            if(i<10)
                this.ddlMonth.Items.Add("0"+i.ToString());
            else
                this.ddlMonth.Items.Add(i.ToString());
        }
        this.ddlMonth.SelectedIndex = 1;  

    }


    public  bool IsDateTime(string source)
    {
        try
        {
            DateTime time = Convert.ToDateTime(source);
            return true;
        }
        catch
        {
            return false;
        }
    }

    protected void PageBind()
    {


        DataSet ds = admissibility.GetAdmissibilityForCheck(Session["UserStreet"].ToString(),"200901");
        gvstreetshow.DataSource = ds;
        lblPageInfo.Text = ds.Tables[0].Rows.Count.ToString();
        gvstreetshow.DataBind();
        DataSet ds1 = admissibility.GetAdmissibilityUnCommit(Session["UserStreet"].ToString());
        gvadmissibilityshow.DataSource = ds1;
        gvadmissibilityshow.DataBind();
    }
    protected void gvstreetshow_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.ToLower() == "del")
        {
            int succ = admissibility.DeleteAdmissibility(Convert.ToInt32(e.CommandArgument.ToString()));
            if (succ > 0)
                PageBind();
            else
                WebHelper.Alert("删除失败，请重新操作！");
        }
        if (e.CommandName.ToLower() == "commit")
        {
            string id = e.CommandArgument.ToString();
            apply.GetApplyById(id);
            apply.apply_passed = 2;
            int succ = apply.UpdateAApplyByApplyId(apply);
            if (succ > 0)
                PageBind();
            else
                WebHelper.Alert("网络延时，请重新操作");
        } if (e.CommandName.ToLower() == "quickcommit")
        {
            string id = e.CommandArgument.ToString();
            string a = "";
            admissibility.SetApplypassed(id);
            int succ = Apply.Update_apply_checkstate_ByApply_id(id, "5");
            int succ2 = Apply.UpdateApply_passedByApply_noid(id, "2");
            if (succ > 0 && succ2 > 0)
                PageBind();
            else
                WebHelper.Alert("网络延时，请重新操作");
        }
    }
    protected void gvstreetshow_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        LinkButton del = (LinkButton)e.Row.FindControl("lkbtndel");
        if (del != null)
            del.Attributes.Add("onclick", "return confirm('您确认要删除此受理单吗？');");

    }
    protected bool AllowView(int noid, object applyid)
    {
        if (applyid == DBNull.Value)
            return false;
        else
        {
            Apply apply = new Apply(applyid.ToString());
            if (apply.apply_passed == 1)
                return true;
            else
                return false;
        }
    }
    protected void gvadmissibilityshow_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.ToLower() == "pass")
        {
            if (admissibility.SetApplypassed(e.CommandArgument.ToString()))
                PageBind();
            else
                WebHelper.Alert("网络延时，请重新操作！");
        }
        if (e.CommandName.ToLower() == "unpass")
        {
            if (admissibility.SetApplyUnpass(e.CommandArgument.ToString()))
                PageBind();
            else
                WebHelper.Alert("网络延时，请重新操作！");
        }
    }
    protected void gvadmissibilityshow_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        LinkButton lbtnpass = (LinkButton)e.Row.FindControl("lkbtnpass");
        if (lbtnpass != null)
            lbtnpass.Attributes.Add("onclick", "return confirm('您确认房产核查通过？');");
        LinkButton lbtnunpass = (LinkButton)e.Row.FindControl("lkbtnunpass");
        if (lbtnunpass != null)
            lbtnunpass.Attributes.Add("onclick", "return confirm('确认房产核查不通过？');");
    }
    protected void gvstreetshow_PageIndexChanged(object sender, EventArgs e)
    {

    }
    //protected void Select2_ServerChange(object sender, EventArgs e)
    //{


    //}
    protected void ddlMonth_SelectedIndexChanged(object sender, EventArgs e)
    {
        
        
        string year = this.Select1.Items[this.Select1.SelectedIndex].Value;
        string mon = this.ddlMonth.SelectedValue;
        string ym = year + mon;
        DataSet ds;
        if (mon == "全部")
        { ds = admissibility.GetAdmissibilityForCheck(Session["UserStreet"].ToString()); }
        else
        {
          ds = admissibility.GetAdmissibilityForCheck(Session["UserStreet"].ToString(), ym);
        }
        
            gvstreetshow.DataSource = ds;
            lblPageInfo.Text = ds.Tables[0].Rows.Count.ToString();
            gvstreetshow.DataBind();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string year = this.Select1.Items[this.Select1.SelectedIndex].Value;
        string mon = this.ddlMonth.SelectedValue;
        if (mon == "全部" || mon == "01")
        {
            WebHelper.Alert("请选择2-12月份");
            return;
        }
        string ym = year + mon;

        SqlParameter[] param = new SqlParameter[1];
        param[0] = new SqlParameter("@YearMonth",int.Parse(ym));
        if(SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, CommandType.StoredProcedure, "BuildDataByYearMonth", param)>1)
          WebHelper.AlertAndRefresh("生成成功");
        else

          WebHelper.AlertAndRefresh("生成失败");

    }
}
